from django import forms
from cashflow.models import *
#from taggit.forms import TagField
#from django.forms.extras.widgets import SelectDateWidget

class AccountForm(forms.ModelForm):
    class Meta:
        model = Account
        exclude = ('user',)
    balance = forms.CharField(max_length=42, help_text="Example: 100.25")
    
    def clean_name(self):
        value = self.cleaned_data['name']
        '''
        try:
            Account.objects.get(name=value)
            raise forms.ValidationError("This account name alredy exist.") 
        except Account.DoesNotExist:
            return value
        '''
        return value
        
class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        exclude = ('user',)
        
            
   
class TransactionForm(forms.Form):
    class Meta:
        model = Transaction
        exclude = ('user',)
        
    def __init__(self, data=None, user=None, *arg, **kwarg):
        self.base_fields['account'].queryset = Account.objects\
                            .filter(user=user)
        self.base_fields['category'].queryset = Category.objects\
                            .filter(user=user)                            
        super(TransactionForm, self).__init__(data, *arg, **kwarg)

    account = forms.ModelChoiceField(None,
        widget=forms.Select(attrs={'class':'text ui-widget-content ui-corner-all'}))
    name = forms.CharField(required=True,
        widget=forms.TextInput(attrs={'class':'text ui-widget-content ui-corner-all'}))
    date = forms.DateField(required=True,
        widget=forms.TextInput(attrs={'class':'text ui-widget-content ui-corner-all'}))
    amount = forms.FloatField(required=True,
        widget=forms.TextInput(attrs={'class':'text ui-widget-content ui-corner-all'}))
    category = forms.ModelChoiceField(None,
        widget=forms.Select(attrs={'class':'text ui-widget-content ui-corner-all'}))
    #tags = TagField(required=False,
    #    widget=forms.TextInput(attrs={'class':'text ui-widget-content ui-corner-all'}))   
    
    description = forms.CharField(widget=forms.Textarea(),required=False)
    
class MoveMoneyForm(forms.Form):
    
    def __init__(self, data=None, from_account=None, user=None, *arg, **kwarg):
        self.base_fields['account'].queryset = Account.objects\
                            .exclude(name__exact=from_account.name)\
                            .filter(user=user)
        super(MoveMoneyForm, self).__init__(data, *arg, **kwarg)

    account = forms.ModelChoiceField(None)
    amount = forms.FloatField()
    #tags = TagField()  
    description = forms.CharField(widget=forms.Textarea())


class FilterHistoryForm(forms.Form):    
    error_css_class = 'error'
    required_css_class = 'required'
    
    date_from = forms.DateField(required=False,
        widget=forms.TextInput(attrs={'class':'text ui-widget-content ui-corner-all'}))
    date_to = forms.DateField(required=False,
        widget=forms.TextInput(attrs={'class':'text ui-widget-content ui-corner-all'}))
    keywords = forms.CharField(required=False,
        widget=forms.TextInput(attrs={'class':'text ui-widget-content ui-corner-all'}))

        
class BudgetForm(forms.ModelForm):
    class Meta:
        model = Budget     
        exclude = ('user',)